#!/bin/bash
export CWD=$(pwd)
tmp=$(mktemp)
rm -f $tmp ; mkdir $tmp
cd $1
if [ ! -d ./DEBIAN ] ; then
    echo "DEBIAN directory not found"
    exit 1
fi
export NAME=$(cat DEBIAN/control | grep "Package: " | sed "s/.*: //g")
export ARCH=$(cat DEBIAN/control | grep "Architecture: " | sed "s/.*: //g")
export VERS=$(cat DEBIAN/control | grep "Version: " | sed "s/.*: //g")
tar czvf $tmp/data.tar.gz [a-z]* | sed "s/^/Adding: /g"
cd DEBIAN
touch md5sums
	find .. | grep -v DEBIAN | while read line
	do
		[ -f $line ] && md5sum $line | sed "s|^\./||g" >> md5sums
	done
tar czf $tmp/control.tar.gz *
cd ../..
echo 2.0 > $tmp/debian-binary
cd $tmp
ar r "${NAME}_${ARCH}_${VERS}.deb" debian-binary control.tar.gz data.tar.gz
mv $tmp/*.deb $CWD/
rm -rf $tmp/
cd $CWD
